package gin

import "net/http"

type (
	ResponseWriter interface {
		http.ResponseWriter
		Status() int
		Written() bool

		// reset 初始化一些状态,如设置状态码和written状态以及赋值http响应
		reset(w http.ResponseWriter)
		setStatus(int)
	}
	responseWriter struct {
		http.ResponseWriter
		status  int
		written bool
	}
)

var _ ResponseWriter = (*responseWriter)(nil)

// WriteHeader 将状态码写入响应头
// 同时更改written状态为true,表示已经写入了响应头
func (w *responseWriter) WriteHeader(code int) {
	w.status = code
	w.written = true
	w.ResponseWriter.WriteHeader(code)
}

// Status 获取响应状态码
func (w *responseWriter) Status() int {
	return w.status
}

func (w *responseWriter) Written() bool {
	return w.written
}

// reset 重置一些状态,如设置状态码和written状态以及赋值http响应
func (w *responseWriter) reset(writer http.ResponseWriter) {
	w.ResponseWriter = writer
	w.status = 0
	w.written = false
}

// setStatus 设置响应状态码
func (w *responseWriter) setStatus(code int) {
	w.status = code
}
